<?php

namespace App\Library\Service;

use App\Admin\AdminUpload;
use App\Library\Logic\ShopImagesLogic;
use App\Library\Logic\ShopUserLogic;
use App\Library\Requests\Upload\UploadImageRequest;
use App\Library\Requests\Upload\UploadRequest;
use App\Library\Requests\Upload\UploadVideoRequest;
use getID3;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\UploadedFile;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;

class UploadService
{
    /**
     * 上传文件
     * @param UploadRequest $request
     * @return JsonResponse
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function index(UploadRequest $request): JsonResponse
    {
        return success(AdminUpload::upload($request->file)->serial_number);
    }

    /**
     * 多图上传
     * @return JsonResponse
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function multiple(): JsonResponse
    {
        $file = request()->file('file');
        if (is_null($file)) return failMessage('请选择要上传的图片');
        $arr = [];
        foreach ($file as $v) $arr[] = AdminUpload::upload($v)->serial_number;
        return success($arr);
    }

    /**
     * image
     * @param UploadImageRequest $request
     * @return JsonResponse
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function image(UploadImageRequest $request): JsonResponse
    {
        $file = $request->file('file');
        if (!in_array($file->getMimeType(), __('mime_type.img'))) return failMessage('图片格式不正确');
        return $this->upload($file, $request->type);
    }

    /**
     * video
     * @param UploadVideoRequest $request
     * @return JsonResponse
     * @throws OssException
     * @throws RequestCore_Exception
     */
    public function video(UploadVideoRequest $request): JsonResponse
    {
        $file = $request->file('file');
        if (!in_array($file->getMimeType(), __('mime_type.video'))) return failMessage('视频格式不正确');
        $getID3 = new getID3;
        $fileInfo = $getID3->analyze($file->getRealPath());
        $seconds = bcadd($fileInfo['playtime_seconds'], 0, 3) * 1;
        if ($seconds > 30) return failMessage('视频时长(' . $seconds . '秒)不能超过30秒');
        return $this->upload($file, $request->type, $fileInfo['fileformat']);
    }

    /**
     * upload
     * @param UploadedFile $file
     * @param int $type
     * @param string|null $ext
     * @return JsonResponse
     * @throws OssException
     * @throws RequestCore_Exception
     */
    private function upload(UploadedFile $file, int $type, ?string $ext = null): JsonResponse
    {
        $uploadFile = AdminUpload::upload($file);
        $shopImages = ShopImagesLogic::insertForModel([
            'userMainId' => MAINID,
            'imgSourceType' => $type,
            'imgType' => is_null($ext) ? 1 : 2,
            'imgUrl' => $uploadFile->serial_number,
            'imgSize' => bcdiv($uploadFile->file_size, 1024, 2),
            'imgExt' => $ext ?? str_replace('image/', '', $uploadFile->mime_type),
            'imgWidth' => $uploadFile->img_width ?? 0,
            'imgHeight' => $uploadFile->img_height ?? 0,
            'createdAt' => time(),
            'updatedAt' => time(),
        ]);
        return success([
            'id' => $shopImages->img_id,
            'serialNumber' => $uploadFile->serial_number,
        ]);
    }
}
